package com.kim.smartring.utils;

import java.util.List;
import java.util.UUID;

import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.util.Log;

import com.kim.smartring.application.SmartRingApplication;
import com.kim.smartring.ble.BleNamesResolver;
import com.kim.smartring.ble.BleWrapper;

public class CallSMSUtils {

	private static final String TAG = "CallSMSUtils";

	public static void sendMsgCountToRing(Context context, String state) {
		SmartRingApplication application = (SmartRingApplication) context
				.getApplicationContext();
		BleWrapper bleWrapper = application.getmBleWrapper();
		if (bleWrapper == null) {
			return;
		}
		if (!bleWrapper.isConnected()) {
			bleWrapper.connect(application.getmDeviceAddress());
		}
		List<BluetoothGattService> bluetoothGattServices = bleWrapper
				.getCachedServices();
		if (bluetoothGattServices == null || bluetoothGattServices.size() == 0) {
			return;
		}
		for (BluetoothGattService bluetoothGattService : bluetoothGattServices) {
			String uuid = bluetoothGattService.getUuid().toString();
			Log.d(TAG, String.format("sendMsgCountToRing->serviceName:",
					BleNamesResolver.resolveServiceName(uuid)));
			if (BleNamesResolver.UUID_CALL_MESSAGE.equals(uuid)) {
				BluetoothGattCharacteristic characteristic = bluetoothGattService
						.getCharacteristic(UUID
								.fromString(BleNamesResolver.UUID_C_CALL_MESSAGE_COUNT));

				bleWrapper.writeDataToCharacteristic(characteristic,
						HexStringUtils.parseHexStringToBytes("0x0101"));
				return;
			}
		}
	}

	public static void sendMsgDetailsToRing(Context context, String msgData) {
		SmartRingApplication application = (SmartRingApplication) context
				.getApplicationContext();
		BleWrapper bleWrapper = application.getmBleWrapper();
		if (bleWrapper == null) {
			return;
		}
		if (!bleWrapper.isConnected()) {
			bleWrapper.connect(application.getmDeviceAddress());
		}
		List<BluetoothGattService> bluetoothGattServices = bleWrapper
				.getCachedServices();
		if (bluetoothGattServices == null || bluetoothGattServices.size() == 0) {
			return;
		}
		for (BluetoothGattService bluetoothGattService : bluetoothGattServices) {
			String uuid = bluetoothGattService.getUuid().toString();
			Log.d(TAG, String.format("sendMsgDetailsToRing->serviceName:",
					BleNamesResolver.resolveServiceName(uuid)));
			if (BleNamesResolver.UUID_CALL_MESSAGE.equals(uuid)) {
				BluetoothGattCharacteristic characteristic = bluetoothGattService
						.getCharacteristic(UUID
								.fromString(BleNamesResolver.UUID_C_CALL_MESSAGE_DETAILS));
				bleWrapper.writeDataToCharacteristic(characteristic,
						HexStringUtils.parseHexStringToBytes(msgData));
				return;
			}
		}
	}
	
}
